<template>
    <rx-layout>
        <div slot="center">
            <a-card :title="i18n('list.officeConvertConf','Office转换器配置')">
                <div style="text-align: right;padding-bottom: 10px;">
                    <a-button-group>
                        <a-button  @click="handleSave">{{$ti18('rxForm.saveBtn','保存')}}</a-button>
                        <a-button v-if="status=='0'" type="primary" @click="handleRun">{{i18n('list.start','启动服务')}}</a-button>
                        <a-button v-if="status=='1'" type="danger" @click="handleEnd">{{i18n('list.stop','停止服务')}}</a-button>
                    </a-button-group>
                </div>
                <a-form-model ref="form" :model="mdl" :rules="rules">
                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol"
                                               :label="i18n('list.officeConvert','Office转换器类型')" prop="fileSystem">
                                <a-radio-group v-model="config.type" @change="defChangeType">
                                    <a-radio v-for="item in officeTypeList"
                                             :value="item.value"
                                             :key="item.value">
                                        {{ item.label }}
                                    </a-radio>
                                </a-radio-group>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item v-if="config.type=='OpenOffice'" :labelCol="labelCol1" :wrapper-col="{ span: 12 }" :label="i18n('form.installPath','安装路径')" prop="installPath">
                                <a-input :placeholder="i18n('form.installPath','安装路径')" v-model="config.installPath"/>
                                <div>{{i18n('msa.pathTips1','说明：需要在服务端安装')}}<a href="http://www.openoffice.org/">OpenOffice</a>{{i18n('msa.pathTips2','服务，并且配置以下信息，可实现在线查看Office文档。')}}</div>
                            </a-form-model-item>

                            <a-form-model-item v-if="config.type=='LibreOffice'" :labelCol="labelCol1" :wrapper-col="{ span: 12 }" :label="i18n('form.installPath','安装路径')" prop="installPath">
                                <a-input :placeholder="i18n('form.installPath','安装路径')" v-model="config.installPath"/>
                                <div>{{i18n('msa.pathTips1','说明：需要在服务端安装')}}<a href="https://www.libreoffice.org/">LibreOffice</a>{{i18n('msa.pathTips2','服务，并且配置以下信息，可实现在线查看Office文档。')}}</div>
                            </a-form-model-item>


                            <a-form-model-item :labelCol="labelCol1" :wrapper-col="{ span: 12 }" label="IP" prop="ip">
                                <a-input placeholder="127.0.0.1"  v-model="config.ip" />
                            </a-form-model-item>

                            <a-form-model-item :labelCol="labelCol1" :wrapper-col="{ span: 12 }" :label="i18n('form.port','端口')" prop="port">
                                <a-input placeholder="8200"  v-model="config.port" />
                            </a-form-model-item>

                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol" :label="i18n('form.enabled','启用')">
                                <a-checkbox  v-model="config.enable" >
                                </a-checkbox>
                            </a-form-model-item>

                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol" :label="i18n('form.autoStart','自动启动')">
                                <a-checkbox  v-model="config.autoStart"  >
                                </a-checkbox>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </a-form-model>
            </a-card>
        </div>
    </rx-layout>
</template>
<script>
import SysFileStoreApi from '@/api/system/core/sysFileStore'
import {RxLayout, BaseFormModel} from "@lowcode/jpaas-common-lib";
import SysOfficeApi from "@/api/system/core/OfficeConfig";
import SysOpenOfficeApi from "@/api/system/core/sysOpenOffice";

export default {
    name: 'OfficeConfig',
    mixins: [BaseFormModel],
    components: {
        RxLayout,
    },
    data() {
        return {
            labelCol: {span: 3},
            labelCol1:{span:4},
            wrapperCol: {span: 21},
            dataObj: {},

            fileSystem: "",
            officeTypeList: [],
            config:{
                port:'',
                ip:'',
                installPath:'',
                autoStart:'',
                enable:'',
                type:'LibreOffice',
            },
            rules: {
                installPath: [{required: false, message: this.i18n('form.installPath','安装路径','input'), trigger: 'change'}],
                ip: [{required: false, message: this.i18n('form.ip','IP','input'), trigger: 'change'}],
                port: [{required: false, message: this.i18n('form.port','端口','input')  , trigger: 'change'}],
            },
            status:'',
        }

    },
    created() {
        this.getOfficeTypeList();
        this.getConfig();
    },
    methods: {
        defChangeType() {
            this.handleStatus();
        },
        getOfficeTypeList() {
            var self_ = this;
            SysOfficeApi.getOfficeTypeList().then(res => {
                var list = res.data;
                for (var key in list) {
                    var temp = list[key];
                    self_.officeTypeList.push({label: temp, value: key});
                }
                //self_.getSysFileStoreNacosConfig();
            });
        },
        i18n(name, text, key) {
            return this.$ti18(name, text, "SysOpenOffice", key);
        },
        getConfig() {
            SysOfficeApi.getConfig().then(res => {
                this.config=JSON.parse(res.message);
                this.handleStatus();
            })
        },
        handleSave() {
            SysOfficeApi.publish(this.config);
        },
        handleRun() {
            SysOfficeApi.start(this.config);
            this.status = 1;
        },
        handleStatus() {
            SysOfficeApi.getStatus(this.config.port.toString(),this.config.type.toString()).then(res => {
                this.status = res.data;
            })
        },
        handleEnd() {
            var self_ = this;
            this.$confirm({
                title: this.$ti18('rxMsa.tipsInfo', '提示信息'),
                content: this.i18n('msa.stopTips', '确认停止服务吗?'),
                okText: this.$ti18('rxForm.ackBtn', '确认'),
                cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                onOk() {
                    SysOfficeApi.stopService(self_.config.port.toString(),self_.config.type.toString());
                    self_.status = 0;
                }
            });
        }
    }
}
</script>
<style>
.relick a {
    margin-right: 10px;
}

</style>